/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.model.player;

import java.io.IOException;

import org.vintagephone.model.Song;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;


/**
 * This class represents media player that is able to scan songs and play a song.
 * 
 * @author Basil Shikin
 *
 */
public class Player 
{
	private static final String Tag = "Player";

	// Controlled objects
	private final MediaPlayer m_player;
	
	private NextSongCallback m_nextSongCallback;
	
	public Player() 
	{
		m_player = new MediaPlayer();
		m_player.setOnCompletionListener( new SongCompletionListener() );
	}

	public void setNextSongCallback(NextSongCallback nextSongCallback)
    {
	    m_nextSongCallback = nextSongCallback; 
    }
	
	/**
	 * Start playing given song
	 * 
	 * @param song Song to play
	 */
	public void play( Song song )
	{
		Log.i( Tag, "Starting to play " + song);
		
		try 
		{
            m_player.reset();
            m_player.setDataSource( song.getFile().getAbsolutePath() );
            
            m_player.prepare();
            m_player.start();
	    } 
		catch (IOException e) 
		{
			Log.e( Tag, "Unable to play " + song, e);
	    }
	}	
	
	
	/**
	 * Pause current song
	 */
	public void pause()
	{
		m_player.pause();
	}
	
	/**
	 * Stop playback
	 */
	public void stop() 
	{
		m_player.stop();
	}


	/**
	 * This interface defines a callback that is invoked when song
	 * is finished playing
	 * 
	 * @author Basil Shikin
	 *
	 */
	public interface NextSongCallback
	{
		void switchToNextSong();
	}
	
	/**
	 * Internal listener for next song 
	 * 
	 * @author Basil Shikin
	 *
	 */
	private class SongCompletionListener
		implements OnCompletionListener
	{
		public void onCompletion(MediaPlayer player) 
		{
			Log.i( Tag, "Song completed, switching to next song");
			
			if ( m_nextSongCallback != null ) m_nextSongCallback.switchToNextSong();
		}
		
	}
}
